/* Original Author:  Dominick O'Dierno
 * Date: 2007   odiernod@gmail.com     --Originally intended for use with DOMercury:  http://www.odierno.com/domercury
 * Description: Handles Options UI for folder indexing
 *
 * --CHANGELOG--
 * 2008-05-18 - Tao Klerks:
 *  - Added "Detailed Logging" checkbox
 * 
 * */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FolderIndexorPlugin
{
    public partial class FolderIndexorOptions : Form
    {
        private int i = 0;
        private List<IndexedFolder> ifr;
        public FolderIndexorOptions(List<IndexedFolder> inf, bool detailedLogging)
        {
            InitializeComponent();
            ifr = inf;
            CreateIFPanels();
            chk_DetailedLogging.Checked = detailedLogging;
        }

        private void CreateIFPanels()
        {
            if ((ifr != null))
            {
                if (ifr.Count > 0)
                {
                    foreach (IndexedFolder f in ifr)
                    {
                        IFPanel ip = new IFPanel(f);
                        tblIFs.Controls.Add(ip);
                        Button b = new Button();
                        b.Text = "Delete";
                        b.Click += new EventHandler(b_Click);
                        tblIFs.Controls.Add(b);
                    }
                }
                else
                {
                    btnNew_Click(new object(), new EventArgs());
                }
            }
            else
            {
                btnNew_Click(new object(), new EventArgs());
            }
        }

        void b_Click(object sender, EventArgs e)
        {
            DeleteRow(((Button)(sender)));
        }

        private void DeleteRow(Button p)
        {
            Control pn = tblIFs.GetNextControl(p, false);
            tblIFs.Controls.Remove(p);
            tblIFs.Controls.Remove(pn);
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            IFPanel ip = new IFPanel();
            tblIFs.Controls.Add(ip);
            Button b = new Button();
            b.Text = "Delete";
            b.Click += new EventHandler(b_Click);
            tblIFs.Controls.Add(b);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            ifr = new List<IndexedFolder>();
            foreach (Control ip in tblIFs.Controls)
            {
                try
                {
                    ifr.Add(((IFPanel)(ip)).IndexedFolder);
                }
                catch
                {
                    //Do nothing, hit a button
                }
            }
            this.Hide();
        }

        public List<IndexedFolder> Folders
        {
            get
            {
                return ifr;
            }
            set
            {
                ifr = value;
            }
        }

        public bool DetailedLogging
        {
            get
            {
                return this.chk_DetailedLogging.Checked;
            }
            set
            {
                this.chk_DetailedLogging.Checked = value;
            }
        }
    }
}